package com.zhfi.dataapply.schedule.writer.market;

import com.zhfi.dataapply.entity.data.po.market.AllotMenPO;
import com.zhfi.dataapply.entity.data.vo.market.AllotMenVO;
import com.zhfi.dataapply.mapper.market.AllotMenMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Component
public class AllotMenWriter implements ItemWriter<AllotMenVO> {

    @Autowired
    private AllotMenMapper allotMenMapper;

    @Override
    public void write(List<? extends AllotMenVO> list) throws Exception {
        for (AllotMenVO vo : list) {
            try {
                List<AllotMenPO> allotMenList = vo.getCompanyAllotMenList();
                if (!CollectionUtils.isEmpty(allotMenList)) {
                    for (AllotMenPO po : allotMenList) {
                        int i = allotMenMapper.updateById(po);
                        if (i <= 0) {
                            allotMenMapper.insert(po);
                        }
                    }
                }
            } catch (Exception e) {
                log.error("AllotMenWriter写入失败，信息：" + vo, e);
            }
        }
    }

}
